﻿using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Standard;
using YSL.Application.Services.LockOrUnLock;
using YSL.Application.Ysl_Dto.Input.LockOrUnlock;
using YSL_Domain.Model;


namespace YSL.Api.Host.Controllers
{
    /// <summary>
    /// 产品锁定控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ProductLockController : ControllerBase
    {
        private readonly IProductLockService _productLockService;
        private readonly ILogger<ProductLockController> _logger;
        private readonly RedisDataDictionaryService  _redisService;

        public ProductLockController(IProductLockService productLockService, ILogger<ProductLockController> logger, RedisDataDictionaryService redisService)
        {
            _productLockService = productLockService;
            _logger = logger;
            _redisService = redisService;
        }
        /// <summary>
        /// 产品锁定
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("AddProductLock")]
        public ApiResult<int> AddProductLock(AddProductLockInput input)
        {

            try
            {
                _logger.LogInformation("产品锁定");
                return _productLockService.AddProductLock(input);
            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);

                throw;
            }
        }

        /// <summary>
        ///  产品锁定列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("GetProductLockList")]
        public ApiPaging<YSL_PRODUCT_LOCK> GetProductLockList([FromQuery]GetProductLockListInput input)
        {
            try
            {
                _logger.LogInformation("查询产品锁定列表");
                return _productLockService.GetProductLockList(input);
            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 查询工单产品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetWorkOrderEntity")]
        public ApiResult<YSL_WORKORDER> GetWorkOrderEntity([FromQuery]AddProductLockInput input)
        {
            try
            {
                _logger.LogInformation("查询工单产品");
                return _productLockService.GetWorkOrderEntity(input);
            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 解锁产品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("UnProductLock")]
        public ApiResult<int> UnProductLock(UnProductLockInput input)
        {
            try
            {
                _logger.LogInformation("解锁产品");
                return _productLockService.UnProductLock(input);
            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改产品锁定时间
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut("UpdateProductLock")]
        public ApiResult<int> UpdateProductLock(UpdateProductLockInput input)
        {
            try
            {
                _logger.LogInformation("修改产品锁定时间");
                return _productLockService.UpdateProductLock(input);
            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 示例：读取数据字典
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet("GetDict")]
        public async Task<IActionResult> GetDict(string type)
        {
            var dict = await _redisService.GetDictionaryAsync<string>(type);
            return Ok(dict);
        }
    }
}
